Ruby on Rails中nokogiri问题的解决方案

本文旨在解决rails server时报错的问题。

起因

最近打算摸索一下Ruby on Rails, 然而一看官方教程运行

1
rails server

错误

出现错误如下:

1
2
Could not find gem 'byebug (>= 0) ruby' in any of the gem sources listed in your Gemfile or available on this machine.
Run `bundle install` to install missing gems.

然后根据提示运行了(注:此处翻墙安装同样会出现错误)

1
bundle install

之后又出现警告:

1
2
3
Installing nokogiri 1.6.6.2 with native extensions

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

错误内容为:

1
2
3
An error occurred while installing nokogiri (1.6.6.2), and Bundler cannot
continue.
Make sure that `gem install nokogiri -v '1.6.6.2'` succeeds before bundling.

按照提示运行了

1
sudo gem install nokogiri -v '1.6.6.2'

之后扔出现ERROR:

1
2
ERROR:  Error installing nokogiri:
invalid gem: package is corrupt, exception while verifying: undefined method `size' for nil:NilClass (NoMethodError) in /Users/mrllover/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/cache/nokogiri-1.6.6.2.gem

解决方案

于是Google了一下,stackoverflow上可选的解决方案有:

Nokogiri error when running bundle install

Error installing Nokogiri on bundle install but already installed

nokogiri will not install - ERROR: Failed to build gem native extension [duplicate]

但之前我遇到过类似问题,当时的解决方案是这样的,请参考Installing Nokogiri

本人机子为mac os,方法如下,输入:

1
gem uninstall nokogiri

如已安装了多个版本会出现如下选择框:

1
2
3
4
Select gem to uninstall:
1. nokogiri-1.6.4.1
2. nokogiri-1.6.5
3. All versions

选择uninstall all versions.

然后输入:

1
xcode-select --install

会下载并安装x-code command line developer tools.

安装完毕后重新安装nokogiri:

1
gem install nokogiri

完成后运行

1
bundle install

此处须知,bundle install的时候需翻墙,别问我为什么,这年头什么都容易被墙,没翻墙会报错.

完成安装后运行:

1
rails server

大功告成,可以开始玩Ruby咯。


如果觉得本文对您有帮助,不妨扫一扫下面的二维码给点打赏呗😌